import {
    nextTick,
    msg,
    confirm,
    close,
    setPageNumber
} from '../../common/utils.js';
import Vue from 'vue';
import {
    serverPath,
    json,
    currentPath
} from '../../common/config.js';
import page from '../../../components/page.vue';
import QuestionService from '../services/questionService.js';
import SubjectService from '../services/subjectService.js';
import QuestionTypeService from '../services/questionTypeService.js';
import _ from "lodash";

window.Module = function () {
    let questionService = new QuestionService();
    let subjectService = new SubjectService();
    let questionTypeService = new QuestionTypeService();

    return new Vue({
        el: '#indexController',
        data: {
            pager: {
                pageIndex: 1,
                pageSize: 500,
                pageCount: 0,
                totalCount: 0,
                pageNumber: []
            },
            list: [],
            searchCondition: {
                name: ''
            },
            selected: [],
            questionTypes: [],
            subjects: []
        },
        methods: {
            init: async function () {
                await this.loadData();
                await this.loadSubject();
                await this.loadQuestionType();
            },
            pageChange: async function (pageIndex, e) {
                this.pager.pageIndex = pageIndex;
                await this.loadData();
            },
            loadData: async function () {
                let res = await questionService.getQuestions(this.pager.pageIndex, this.pager.pageSize, this.searchCondition);

                if (!res.type) {
                    return;
                }
                else if (json === res.type) {
                    var response = res.body;
                    if ('Success' === response.result) {
                        this.list = response.data.data;
                        for (let item in response.data) {
                            if ('data' !== item) {
                                this.pager[item] = res.body.data[item];
                            }
                        }
                        var self = this;
                        _.forEach(this.list, function (value) {
                            var index = _.findIndex(self.selected, function (o) {
                                return o.id == value.id;
                            });
                            if (index > 0)
                                value.selected = true;
                        });
                        setPageNumber(this.pager);
                    }
                    else {
                        await msg(response.message);
                    }
                }
            },
            selectQuestion: async function (item) {
                if (!item)
                    return;
                if (item.selected)
                    this.selected.push(item);
                else {
                    _.remove(this.selected, function (o) {
                        return o.id == item.id;
                    });
                }
            },
            loadSubject: async function () {
                let res = await subjectService.getSubject(1, 1000);

                if (!res.type) {
                    return;
                }
                else if (json === res.type) {
                    var response = res.body;
                    if ('Success' === response.result) {
                        this.subjects = response.data.data;
                    }
                    else {
                        await msg(response.message);
                    }
                }
            },
            loadQuestionType: async function () {
                let res = await questionTypeService.getQuestionType();

                if (!res.type) {
                    return;
                }
                else if (json === res.type) {
                    var response = res.body;
                    if ('Success' === response.result) {
                        this.questionTypes = response.data;
                    }
                    else {
                        await msg(response.message);
                    }
                }
            },
            confirm: function () {
                parent.result = this.selected;
                this.close();
            },
            close: function () {
                close();
            }
        },
        components: {
            "page": page
        }
    });
};